package com.Lixiaoqian.PartyHistoryMuseum.utils;


import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Build;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

/**
 * 动画工具类(属性动画)
 * Created by HuangZhiBo on 2017/4/11/011.
 */

public class AnimUtils {

    private static ObjectAnimator objectAnimator;

    public static void startAnim(View view, AnimBuild animBuild){
        objectAnimator = ObjectAnimator.ofFloat(view, animBuild.getAnimName(),animBuild.getValue()[0], animBuild.getValue()[1]);
        objectAnimator.setDuration(animBuild.getDuration());
        objectAnimator.setInterpolator(animBuild.getInterStyle());
        if (animBuild.isRESTART()){
            objectAnimator.setRepeatCount(-1);//设置动画重复次数
            objectAnimator.setRepeatMode(ValueAnimator.RESTART); //动画重复模式
        }

        objectAnimator.start();
    }


    public static void pauseAinm(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            objectAnimator.pause();
        }
    }
    /**
     * Build模式
     */
    public static class AnimBuild{
        private int duration=1000; //播放时长
        private android.view.animation.Interpolator Interpolator=new LinearInterpolator(); //差值器
        private float interSpeech =1.0f; //inter的速率
        private String AnimName="translationY"; //view属性->如背景颜色(backgroundColor...)
        private String[] AnimMold=new String[]
                {"translationY","translationX","alpha","rotation","scaleX"};//scaleX缩放,rotation旋转,alpha渐变
        private float[] value;  //播放动画参数

        private   boolean RESTART=false;

        public boolean isRESTART() {
            return RESTART;
        }

        public AnimBuild setRESTART(boolean RESTART) {
            this.RESTART = RESTART;
            return this;
        }

        public int getDuration() {
            return duration;
        }

        public AnimBuild setDuration(int duration) {
            this.duration = duration;
            return this;
        }

        public Interpolator getInterStyle() {
            return Interpolator;
        }

        public AnimBuild setInterStyle(int interStyle) {
            switch (interStyle){
                case 0:  //匀速
                    this.Interpolator=new LinearInterpolator();
                    break;
                case 1:  //加速
                    this.Interpolator=new AccelerateInterpolator(interSpeech);
                    break;
                case 2:  //减速
                    this.Interpolator=new DecelerateInterpolator(interSpeech);
                    break;
                case 3:  //动画结束弹跳
                    this.Interpolator=new BounceInterpolator();
                    break;
            }
            return this;
        }


        public AnimBuild setInterSpeech(float interSpeech) {
            this.interSpeech = interSpeech;
            return this;
        }

        public String getAnimName() {
            return AnimName;
        }

        /**
         * 设置动画属性
         * @param name
         * @return
         */
        public AnimBuild setAnimName(String name) {
            this.AnimName=name;
            return this;
        }

        /**
         * 选择动画属性
         * @param animMold
         * @return
         */
        public AnimBuild selectAnimName(int animMold) {
            this.AnimName= AnimMold[animMold];
            return this;
        }

        public float[] getValue() {
            return value;
        }

        public AnimBuild setValue(float... value) {
            this.value = value;
            return this;
        }

        public void setIsReStart(boolean is){
           RESTART=is;
        }
    }
}
